var app = angular.module('app',['ngRoute']);

app.debug = true;

function log(log){
    if(app.debug){
        console.log(log);
    }
}

app.config(['$controllerProvider', '$compileProvider', '$filterProvider', '$provide', '$routeProvider', '$locationProvider',
    function($controllerProvider, $compileProvider, $filterProvider, $provide, $routeProvider, $locationProvider) {
    app.register = {
        controller: $controllerProvider.register,
        directive: $compileProvider.directive,
        filter: $filterProvider.register,
        factory: $provide.factory,
        service: $provide.service
    };
    app.asyncJs = function (js) {
        return ["$q", "$rootScope", function ($q,$rootScope) {
            var deferred = $q.defer();
            $script(js, function () {
                $rootScope.$apply(function () {
                    deferred.resolve();
                });
            });
            return deferred.promise;
        }];
    };
    $routeProvider
        .when('/index', {
            templateUrl: 'static/tpl/index.html',
            resolve: {
                load: app.asyncJs('static/controller/index.js')
            }
        })
        .when('/list/', {
            templateUrl: 'static/tpl/list.html',
            resolve: {
                load: app.asyncJs(['static/js/vendor/moment.js', 'static/directive/mbdatepicker.js','static/controller/list.js'])
            }
        })
        .when('/detail/:pageId', {
            templateUrl: 'static/tpl/detail.html',
            resolve: {
                load: app.asyncJs(['static/controller/detail.js'])
            }
        })
        .when('/entrance', {
            templateUrl: 'static/tpl/entrance.html',
            resolve: {
                load: app.asyncJs('static/controller/entrance.js')
            }
        })
        .otherwise({
            redirectTo: '/index'
        });

        $locationProvider.html5Mode(true);
}]);